package com.csu.jpetstore_reconfiguration_back.Controller;
import com.csu.jpetstore_reconfiguration_back.common.CommonResponse;
import com.csu.jpetstore_reconfiguration_back.domain.User;
import com.csu.jpetstore_reconfiguration_back.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Valid
public class UserController {
    @Resource
    private UserService userService;
    //登录
    @PostMapping("/tokens")
    public CommonResponse<String> login(@Validated @RequestBody User user){
        return userService.login(user.getUsername(),user.getPassword());
    }

    //注册
    @PostMapping("/users")
    public CommonResponse<User> register(@Validated @RequestBody User user){
        return userService.register(user);
    }

    //获取当前用户
    @GetMapping("/users/current")
    public CommonResponse<User> getCurrentUser(HttpServletRequest request){
        return userService.getCurrentUser(request);
    }

    //修改
    @PostMapping("/users/{id}")
    public String update(@PathVariable String id){
        return "update";
    }

    //获取所有用户信息
    @GetMapping("/users")
    public CommonResponse<List<User>> getAllUser(){
        //do:鉴权，仅管理员可用
        return userService.getAllUser();
    }

    //获取单个用户信息
    @GetMapping("/users/{id}")
    public String getOne(@PathVariable String id){
        return "getOne";
    }

    //删除
    @PostMapping("/users/{id}/delete")
    public String delete(@PathVariable String id){
        return "delete";
    }

    //获取角色权限
    @GetMapping("/users/{id}/permission")
    public String getRole(@PathVariable String id){
        return "getRole";
    }

    //修改角色权限
    @PostMapping("/users/{id}/permission")
    public String updateRole(@PathVariable String id){
        return "updateRole";
    }


}
